<template>
    <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="楼层名称" prop="">
            <el-input v-model="AddFloor.name"></el-input>
        </el-form-item>

        <el-form-item v-if="dialogVisible" label="选择楼栋" prop="">
            <el-dropdown  split-button type="primary">
                所属楼栋：{{myCurrentBuilding}}
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item v-for="(item, index) in BuildingData" :key="index" @click="changeBuilding(item.id,item.name)">{{ item.name }}</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
        </el-dropdown>
        </el-form-item>
        
        

        <div style="text-align: center;margin-top: 20px;">
            <el-button style="margin-right: 50px;" @click="Close()">取消</el-button>
            <el-button type="primary" @click="SubmitForm()">确定</el-button>
        </div>
    </el-form>
</template>

<script setup>
import service from '@/request';
import { ElMessage } from 'element-plus';
import { defineProps, onBeforeMount, reactive,ref } from 'vue'

//获取楼栋数据
let BuildingData =reactive([]);
let myCurrentBuilding=ref('查找中');

//用户提交数据
let AddFloor=reactive({
    id:1,
    name:'',
    buildingId:1
})


async function getDataList() {
    
    //楼栋数据请求
    service.get("/Building/getAllBuilding").then(
        async (res) => {
            BuildingData.push(...res.value)
            if(BuildingData.length > 0){
                myCurrentBuilding.value=BuildingData[0].name;
            }
            
        }
    )

}
//更改楼栋
async function changeBuilding(id,name){
    AddFloor.buildingId=id;
    AddFloor.id=id;
    myCurrentBuilding.value=name;
}


// #region 显示提示
async function ShowTip(isSuccess, msg) {
    if (isSuccess == true) {
        ElMessage({ message: msg, type: 'success' })
    }
    else {
        ElMessage({ message: msg, type: 'error' })
    }
}
//#endregion

//控制是否打开楼栋列表
let dialogVisible = ref(false)

//传入行参数
let props = defineProps(["Close", "transmitObj"])

let formData = reactive
({
    id: 0,
    name: '',
    state:false
})


async function SubmitForm() {
    if (AddFloor.name == '') {
        ShowTip(false,'请先完成必要步骤')
        return
    }

  
    //添加
    if (props.transmitObj == null) {
            await service.post(`/Floor/addFloor`,AddFloor).then(
                async res => {
                    ShowTip(true,"操作成功")
                },
                error => {
                    ShowTip(false,error)
                }
            )
    }
    //编辑
    else {

        //捕获传入的楼层id
        AddFloor.buildingId=props.transmitObj.id;
        AddFloor.id=props.transmitObj.id;

        await service.post("/Floor/updateFloor",AddFloor).then(
            async res => {
                ShowTip(true,"操作成功")
            },
            error => {
                ShowTip(false,error)
            }
        )
    }
    await props.Close()
}

async function Reload(){
   await getDataList();
}

onBeforeMount(async () => {
    await Reload()
    //如果是编辑操作就初始化
    if (props.transmitObj != null) {
        for (var p in props.transmitObj) {
            formData[p] = props.transmitObj[p]
        }
    }

    //说明是增加楼层
    if (props.transmitObj == null)
    {
        dialogVisible.value=true;
    }
})
</script>

<style scoped>
</style>